// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Horiz Crusher Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 70 : TYPE_HORIZCRUSHER

// Function declarations
reserve function HorizCrusher_DebugDraw
reserve function HorizCrusher_DebugSpawn
reserve function HorizCrusher_Function156

// Static Values
public value HorizCrusher_value36 = 0;

// Tables

function HorizCrusher_DebugDraw
	temp7 = object[0].value17
	temp7 -= HorizCrusher_value36
	temp5 = object.xpos
	switch temp7
	case 0
		temp6 = 0x380000
		break
	case 1
		temp6 = 0x9F0000
		break
	case 2
		temp6 = 0x500000
		break
	case 3
		temp6 = 0x380000
		break
	end switch
	if object.direction == FLIP_NONE
		FlipSign(temp6)
	end if
	temp5 += temp6
	if temp5 > object.xpos
		temp0 = temp5
		temp0 -= object.xpos
	else
		temp0 = object.xpos
		temp0 -= temp5
	end if
	temp0 >>= 16
	temp0 += 4
	temp1 = 299
	temp1 -= temp0
	if object.direction == FLIP_X
		temp2 = temp0
		temp2 += 34
		FlipSign(temp2)
	else
		temp2 = 16
	end if
	temp0 += 17
	EditFrame(3, temp2, -8, temp0, 16, temp1, 388)
	DrawSpriteXY(3, temp5, object.ypos)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, FX_FLIP, temp5, object.ypos)
	DrawSpriteFX(2, FX_FLIP, temp5, object.ypos)
end function


function HorizCrusher_DebugSpawn
	temp7 = object[0].value17
	temp7 -= HorizCrusher_value36
	CreateTempObject(TypeName[Horiz Crusher], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].value2 = 0
	if object.direction == FLIP_NONE
		object[tempObjectPos].value8 = -0x7000
	else
		object[tempObjectPos].value8 = 0x7000
	end if
	object[tempObjectPos].value3 = object.xpos
	object[tempObjectPos].value4 = object[tempObjectPos].value3
	switch temp7
	case 0
		temp6 = 0x380000
		break
	case 1
		temp6 = 0x9F0000
		break
	case 2
		temp6 = 0x500000
		break
	case 3
		temp6 = 0x380000
		break
	end switch
	if object.direction == FLIP_NONE
		FlipSign(temp6)
	end if
	object[tempObjectPos].value4 += temp6
	if object[tempObjectPos].value2 == 1
		object[tempObjectPos].xpos = object[tempObjectPos].value4
	end if
end function


function HorizCrusher_Function156
	checkResult = 0
	temp0 = object.ypos
	temp0 >>= 16
	temp0 -= screen.cameraY
	Abs(temp0)
	if 64 <= screen.ycenter
		temp1 = object.value4
		temp1 -= object.value3
		temp1 >>= 16
		temp1 += 8
		temp1 >>= 1
		temp0 = object.value3
		temp0 += object.value4
		temp0 >>= 17
		temp0 -= 12
		temp0 -= screen.cameraX
		Abs(temp0)
		temp0 -= temp1
		temp0 -= 40
		if temp0 <= screen.xcenter
			checkResult = 1
		end if
	end if
end function


event ObjectMain
	if object.value2 == 1
		if object[+1].propertyValue == 0
			object.value7 = 0
			object.xvel += object.value8
			object.xpos += object.xvel
			temp0 = 0
			if object.direction == FLIP_NONE
				if object.xpos <= object.value4
					temp0 = 1
				end if
			else
				if object.xpos >= object.value4
					temp0 = 1
				end if
			end if
			if temp0 == 1
				object.xpos = object.value4
				object.xvel = 0
			end if
		else
			if object.value8 > 0
				object.xpos -= 0x8000
			else
				object.xpos += 0x8000
			end if
			temp0 = 0
			if object.direction == FLIP_NONE
				if object.xpos >= object.value3
					temp0 = 1
				end if
			else
				if object.xpos <= object.value3
					temp0 = 1
				end if
			end if
			if temp0 == 1
				object.xpos = object.value3
				object.xvel = 0
			end if
		end if
	else
		switch object.state
		case 0
			object.value7 = 0
			object.xvel += object.value8
			object.xpos += object.xvel
			temp0 = 0
			if object.direction == FLIP_NONE
				if object.xpos <= object.value4
					temp0 = 1
				end if
			else
				if object.xpos >= object.value4
					temp0 = 1
				end if
			end if
			if temp0 == 1
				object.xpos = object.value4
				object.xvel = 0
				object.state++
			end if
			break
		case 1
			object.value0++
			if object.value0 == 60
				object.value0 = 0
				if object.value8 > 0
					object.xvel = -0x8000
				else
					object.xvel = 0x8000
				end if
				object.state++
			end if
			break
		case 2
			object.xpos += object.xvel
			temp0 = 0
			if object.direction == FLIP_NONE
				if object.xpos >= object.value3
					temp0 = 1
				end if
			else
				if object.xpos <= object.value3
					temp0 = 1
				end if
			end if
			if temp0 == 1
				object.xpos = object.value3
				object.xvel = 0
				object.state++
			end if
			break
		case 3
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.state = 0
			end if
			break
		end switch
	end if
	object.value5 = object.xpos
	object.value5 &= -0x10000
	object.value5 -= object.value6
	temp0 = object.xpos
	object.xpos = object.value6
	temp5 = object.value5
	temp5 >>= 16
	if object.direction == FLIP_NONE
		temp6 = -42
		temp6 += temp5
		temp7 = -12
		temp7 += temp5
	else
		temp6 = 12
		temp6 += temp5
		temp7 = 42
		temp7 += temp5
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0				
			BoxCollisionTest(C_BOX, object.entityPos, -12, -32, 12, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].xpos += object.value5
				break
			case 2
				if object.value5 < 0
					object[currentPlayer].xpos += object.value5
				end if
				break
			case 3
				if object.value5 > 0
					object[currentPlayer].xpos += object.value5
				end if
				break
			case 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(PlayerObject_Kill)
				end if
				break
			end switch
			BoxCollisionTest(C_TOUCH, object.entityPos, temp6, -24, temp7, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_Hit)
			end if
		end if	
	next
	object.xpos = temp0
	object.value6 = object.xpos
	object.value6 &= -0x10000
end event


event ObjectDraw
	if object.xpos > object.value3
		temp0 = object.xpos
		temp0 -= object.value3
	else
		temp0 = object.value3
		temp0 -= object.xpos
	end if
	temp0 >>= 16
	temp0 += 4
	temp1 = 299
	temp1 -= temp0
	if object.direction == FLIP_X
		temp2 = temp0
		temp2 += 34
		FlipSign(temp2)
	else
		temp2 = 16
	end if
	temp0 += 17
	EditFrame(3, temp2, -8, temp0, 16, temp1, 388)
	DrawSpriteXY(3, object.value6, object.ypos)
	DrawSpriteFX(0, FX_FLIP, object.value3, object.ypos)
	DrawSpriteFX(1, FX_FLIP, object.value6, object.ypos)
	DrawSpriteFX(2, FX_FLIP, object.value6, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(36, -16, 8, 32, 289, 158)
	SpriteFrame(-12, -32, 32, 64, 256, 142)
	SpriteFrame(-43, -24, 31, 48, 256, 207)
	SpriteFrame(-92, -8, 184, 16, 132, 388)
	foreach (TypeName[Horiz Crusher], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value2 = object[arrayPos0].propertyValue
		object[arrayPos0].value2 >>= 7
		GetBit(object[arrayPos0].direction, object[arrayPos0].propertyValue, 6)
		object[arrayPos0].value8 = -0x7000
		object[arrayPos0].state = object[arrayPos0].propertyValue
		object[arrayPos0].state &= 3
		object[arrayPos0].value3 = object[arrayPos0].xpos
		object[arrayPos0].value4 = object[arrayPos0].value3
		switch object[arrayPos0].state
		case 0
			temp0 = 0x380000
			break
		case 1
			temp0 = 0x9F0000
			break
		case 2
			temp0 = 0x500000
			break
		case 3
			temp0 = 0x380000
			break
		end switch
		if object[arrayPos0].direction == FLIP_NONE
			FlipSign(temp0)
		end if
		object[arrayPos0].value4 += temp0
		if object[arrayPos0].value2 == 1
			object[arrayPos0].xpos = object[arrayPos0].value4
		end if
	next
	temp0 = 0
	HorizCrusher_value36 = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Horiz Crusher], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(HorizCrusher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(HorizCrusher_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
